package com.aw.baselib.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aw.baselib.R;
import com.aw.baselib.base.AwBaseApplication;

import java.util.List;

/**
 * Created by hzw
 */

public class AwViewUtil {

    /**
     * @category 让EditText内的密码显示明文或显示mask符号
     */
    public static void setEditTxtVisibility(EditText et, boolean flag) {
        int type = flag ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                : (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        et.setInputType(type);
    }

    public static void setTopView(Context context, RadioButton view, int drawableId) {
        Drawable top = context.getResources().getDrawable(drawableId);
        top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
        view.setCompoundDrawables(null, top, null, null);
    }

    public static void setRightView(Context context, TextView view, int drawableId) {
        Drawable drawable = context.getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(null, null, drawable, null);
    }

    public static void setEnableView(List<EditText> etList, EditText et, TextView tv) {
        AwEditTextListener.isChangeListener(et, changed -> {
            if(changed) {
                if(AwDataUtil.isEmpty(etList)) {
                    tv.setTextColor(AwBaseApplication.getInstance().getResources().getColor(R.color.white));
                    tv.setBackground(AwBaseApplication.getInstance().getDrawable(R.drawable.bg_common_btn_gray));
                    return;
                }
                boolean isPass = true;
                for(EditText item : etList) {
                    if(AwDataUtil.isEmpty(item.getText().toString())) {
                        isPass = false;
                        break;
                    }
                }
                if(isPass) {
                    tv.setTextColor(AwBaseApplication.getInstance().getResources().getColor(R.color.white));
                    tv.setBackground(AwBaseApplication.getInstance().getDrawable(R.drawable.bg_common_btn_green));
                } else {
                    tv.setTextColor(AwBaseApplication.getInstance().getResources().getColor(R.color.white));
                    tv.setBackground(AwBaseApplication.getInstance().getDrawable(R.drawable.bg_common_btn_gray));
                }
            } else {
                tv.setTextColor(AwBaseApplication.getInstance().getResources().getColor(R.color.white));
                tv.setBackground(AwBaseApplication.getInstance().getDrawable(R.drawable.bg_common_btn_gray));
            }
        });

    }
}
